<?php include APPLICATION_PATH . '/modules/default/layouts/scripts/content-outer-start.phtml'; ?>
<script>
    function calTotal() {
        var res = 0;

        $(".quantity").each(function() {
            if ($(this).attr('disabled') == undefined) {
                var price = parseInt($(this).attr("price"), 10);
                var quantity = parseInt($(this).val(), 10);
                var thue = parseInt($("#" + $(this).attr("thue")).val(), 10);

                if (!isNaN(price) && !isNaN(quantity))
                {
                    var temp = price * quantity;
                    temp += temp * (thue / 100);
                    res += temp;
                }
            }
        });

        $("#total").html(res);
    }
    function selectItem(cb, soluong, thue) {
        if (cb.checked == true) {
            $('#' + soluong).removeAttr('disabled');
            $('#' + thue).removeAttr('disabled');
        } else {
            $('#' + soluong).attr('disabled', '');
            $('#' + thue).attr('disabled', '');
        }
        calTotal();
    }
    function editDaily() {
        var code_daily = $('#daily').val();
        $.ajax({
            url: '<?php echo $this->baseUrl ?>/default/donhang/getsanpham',
            type: "POST",
            data: {
                code_daily: code_daily
            },
            dataType: "json",
            success: function(resultData) {
                if (resultData.ErrorMessage !== "-1") {
                    var content = "";
                    content += '<tr class="GridHeader" align="center">'
                            + '<th align="center" scope="col"></th>'
                            + '<th align="center" scope="col">Mã sản phẩm</th>'
                            + '<th align="center" scope="col">Tên sản phẩm</th>'
                            + '<th align="center" scope="col">Đơn giá</th>'
                            + '<th align="center" scope="col">Số lượng</th>'
                            + '<th align="center" scope="col">Thuế</th>'
                            + '</tr>';
                    for (var i = 0; i < resultData.result.length; i++) {
                        content += '<tr class="GridItem">'
                                + '<td align="center">'
                                + '<input type="checkbox" onclick="selectItem(this,\'soluong' + i + '\',\'thue' + i + '\');"/>'
                                + '</td>'
                                + '<td align="center">'
                                + resultData.result[i]['MaSanPham']
                                + '</td>'
                                + '<td align="center">'
                                + resultData.result[i]['TenSanPham']
                                + '</td>'
                                + '<td align="center" style="width:300px;">'
                                + resultData.result[i]['DonGia']
                                + '</td>'
                                + '<td align="center">'
                                + '<input disabled thue="thue' + i + '" id="soluong' + i + '" name="sl' + resultData.result[i]['MaSanPham'] + '" type="text" maxlength="5" class="TextBox quantity" price="' + resultData.result[i]['DonGia'] + '" procode="1" autocomplete="off" value="0" onchange="calTotal();" style="width:10%;">'
                                + '</td>'
                                + '<td align="center">'
                                + '<input disabled id="thue' + i + '" name="th' + resultData.result[i]['MaSanPham'] + '" type="text" maxlength="5" class="TextBox" price="' + resultData.result[i]['DonGia'] + '" procode="1" autocomplete="off" value="0" onchange="calTotal();" style="width:10%;">%'
                                + '</td>'
                                + '</tr>';
                    }
                    content += '<tr>'
                            + '<td colspan="6" align="right">'
                            + 'Thành tiền: <span id="total">0</span> VND'
                            + '</td>'
                            + '</tr>';
                    $("#main_table").html(content);
                }
            }
        });

    }
    $(document).ready(function() {
        editDaily();
        Calendar.setup({
            weekNumbers: true, //display week number
            inputField: ngayban, //id of element that is filled by jcal2 
            trigger: ngayban_trigger, //id of element that triggers jcal2
            onSelect: function() { //action occurs when user picks a date
                this.hide();
            },
            showTime: 24, //24 hrs mode
            dateFormat: "%d/%m/%Y %H:%M:00"
        });
        var trigger = document.getElementsByClassName("trigger");
        for (var i = 0; i < trigger.length; i++) {
            trigger[i].addEventListener("click", function(e) {
                e.preventDefault();
            }, false);
        }
        <?php
        if ($this->success) {
            echo 'alert("Nhập đơn hàng thành công.");';
        }
        ?>
    });
</script>
<div align="center">
    <form id="mainform" action="<?php echo $this->baseUrl . '/default/donhang/index' ?>" method="post">
        <div class="form_row">
            
            <!--<div class="form_row">
                <div class="label_name_1"><label for="owner" class="label_css">Đại lý bán</label> </div>
                <div class="float_l">
                    <select id="daily" name="dl_ban" class="select_css" style="background-color: #FFFFFF">
                        <?php
//                        if ($this->daily) {
//                            foreach ($this->daily as $item) {
//                                echo '<option value="' . $item['MaDaiLy'] . '">' . $item['TenDaiLy'] . '</option>';
//                            }
//                        }
                        ?>
                    </select>
                </div>
            </div>
            <div class="form_row">
                <div class="label_name_1"><label for="owner" class="label_css">Đại lý mua</label> </div>
                <div class="float_l">
                    <select id="daily" name="dl_mua" class="select_css" style="background-color: #FFFFFF">
                        <?php
//                        if ($this->daily) {
//                            foreach ($this->daily as $item) {
//                                echo '<option value="' . $item['MaDaiLy'] . '">' . $item['TenDaiLy'] . '</option>';
//                            }
//                        }
                        ?>
                    </select>
                </div>
            </div>-->
            <div class="form_row">
                <div class="label_name_1"><label for="owner" class="label_css">Mã số khách hàng</label> </div>
                <div class="float_l">
                    <input type="text" id="daily" name="dl_gioithieu" class="input_css" style="background-color: #FFFFFF" placeholder="Nhập chính mã số khách hàng">
<!--                    <select id="daily" name="dl_gioithieu" class="select_css" style="background-color: #FFFFFF">
                        <?php
//                        if ($this->daily) {
//                            foreach ($this->daily as $item) {
//                                echo '<option value="' . $item['MaDaiLy'] . '">' . $item['TenDaiLy'] . '</option>';
//                            }
//                        }
                        ?>
                    </select>-->
                </div>
            </div>
            <div class="form_row">
                <div class="label_name_1"><label for="owner" class="label_css">Ngày mua hàng</label> </div>
                <div class="float_l">
                    <input type="text" id="ngayban" name="ngayban" readonly="readonly" class="input_css" placeholder="nhập ngày mua hàng">
                </div>
                <div class="float_l">
                    <a href="#" id="ngayban_trigger" class="calendar trigger"></a>
                </div>
            </div>
            
            <div class="form_row">
                <table class="Grid" cellspacing="0" cellpadding="3" rules="all" border="1" id="ctl00_ContentPlaceHolder1_PupilFault_grdFault" style="width:100%;border-collapse:collapse;">
                    <tbody id="main_table">



                    <input type="hidden" name="pro_qua" id="pro_qua" value=""/>
                    </tbody>
                </table>
            </div>
                               
            <div class="form_row" align="center">
                <input type="submit" id="save" value="Nhập" class="button gray">
            </div>            
        </div>         
    </form>    
</div>

<?php include APPLICATION_PATH . '/modules/default/layouts/scripts/content-outer-end.phtml'; ?>